package com.europe.business.europebusiness.ui.adapter.viewholder;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * recyview的adapter通用viewholder类
 */
public class ListViewHolder extends RecyclerView.ViewHolder {

    private SparseArray<View> views;
    private View mConvertView;
    private Context mContext;


    public ListViewHolder(Context context, @NonNull View itemView, ViewGroup parent) {
        super(itemView);
        views = new SparseArray<View>();
        mContext = context;
        mConvertView = itemView;
    }

    /**
     * 创建一个ViewHolder并返回
     * @param context
     * @param layoutId
     * @param parent
     * @return
     */
    public static ListViewHolder getViewHold(Context context, int layoutId, ViewGroup parent) {
        View view = LayoutInflater.from(context).inflate(layoutId, parent, false);
        ListViewHolder viewHolder = new ListViewHolder(context, view, parent);
        return viewHolder;
    }


    public <T extends View> T getView(int viewId) {
        View view = views.get(viewId);
        if (view == null) {
            view = mConvertView.findViewById(viewId);
            views.put(viewId, view);
        }
        return (T) view;
    }
}